
// 测试webrtc-audio-processing中vad功能
// 时间2020-10-26
// 编译成功，还没测试

#include <iostream>
using namespace std;

// 使用common_audio中的vad
#include "webrtc/common_audio/vad/include/webrtc_vad.h"
// vad.h用于构建自己的vad检测程序

// 使用modules模块中的vad
#include "webrtc/modules/audio_processing/vad/voice_activity_detector.h"

#include <stdlib.h>
#include <stdio.h>
#include "wavreader.h"

#define framelen 480
int main(int argc, char* argv[]) {
    VadInst* myvad1;
    myvad1 = WebRtcVad_Create();
    WebRtcVad_Init(myvad1);
    WebRtcVad_set_mode(myvad1, 3);
    webrtc::VoiceActivityDetector myvad2;
    int mod = atoi(argv[3]);

    struct wavdata* d = getdatafromWAV(argv[1]);
    float* ind = d->data[0];
    int timesteps = d->samples_per_channel / framelen;
    if (mod == 1) {
        struct wavdata* out_data = initwavdata(2, d->samples_per_channel, d->samplerate, 32);
        out_data->data[0] = ind;
        float* vadout1 = (float*)malloc(sizeof(float) * d->samples_per_channel);
        out_data->data[1] = vadout1;
        int vadres = 0;
        short block[framelen];
        for (int i = 0; i < timesteps; i++) {
            for (int j = 0; j < framelen; j++) {
                block[j] = ind[i * framelen + j] * 32768;
            }
            vadres = WebRtcVad_Process(myvad1, d->samplerate, block, framelen);
            for (int j = 0; j < framelen; j++) {
                vadout1[i * framelen + j] = vadres;
            }
        }
        writeWAV_float(argv[2], out_data);
    }
    if (mod == 2) {
        struct wavdata* out_data = initwavdata(2, d->samples_per_channel, d->samplerate, 32);
        out_data->data[0] = ind;
        float* vadout2 = (float*)malloc(sizeof(float) * d->samples_per_channel);
        out_data->data[1] = vadout2;
        float vadres2 = 0;
        short block[framelen];
        for (int i = 0; i < timesteps; i++) {
            for (int j = 0; j < framelen; j++) {
                block[j] = ind[i * framelen + j] * 32768;
            }
            for (int k = 0; k < 3; k++) {
                myvad2.ProcessChunk(block + k * framelen / 3, framelen / 3, d->samplerate);
                vadres2 = myvad2.last_voice_probability();
                for (int l = 0; l < framelen / 3; l++) {
                    vadout2[i * framelen + k * framelen / 3 + l] = vadres2;
                }
            }
        }
        writeWAV_float(argv[2], out_data);
    }
    if (mod == 3) {
        struct wavdata* out_data = initwavdata(3, d->samples_per_channel, d->samplerate, 32);
        out_data->data[0] = ind;
        float* vadout1 = (float*)malloc(sizeof(float) * d->samples_per_channel);
        out_data->data[1] = vadout1;
        float* vadout2 = (float*)malloc(sizeof(float) * d->samples_per_channel);
        out_data->data[2] = vadout2;

        int vadres = 0;
        float vadres2 = 0;
        short block[framelen];
        for (int i = 0; i < timesteps; i++) {
            for (int j = 0; j < framelen; j++) {
                block[j] = ind[i * framelen + j] * 32768;
            }
            vadres = WebRtcVad_Process(myvad1, d->samplerate, block, framelen);
            for (int k = 0; k < 3; k++) {
                myvad2.ProcessChunk(block + k * framelen / 3, framelen / 3, d->samplerate);
                vadres2 = myvad2.last_voice_probability();
                for (int l = 0; l < framelen / 3; l++) {
                    vadout2[i * framelen + k * framelen / 3 + l] = vadres2;
                }
            }
            for (int j = 0; j < framelen; j++) {
                vadout1[i * framelen + j] = vadres;
            }
        }
        writeWAV_float(argv[2], out_data);
    }

    return 0;
}

/*
int main(int argc, char* argv[])
{   
    // method 1
    VadInst *myvad1;
    myvad1 = WebRtcVad_Create();
    WebRtcVad_Init(myvad1);
    WebRtcVad_set_mode(myvad1, 0);
    
    // method 2
    webrtc::VoiceActivityDetector myvad2;

    // method 3
    

    FILE *fidr = NULL;
    fidr = fopen(argv[1], "rb");
    short indata[160] = {0}; // 10ms数据    
    FILE *fidw[2] = {NULL};
    fidw[0] = fopen("vaddetection1.dat", "wb");
    fidw[1] = fopen("vaddetection2.dat", "wb");    

    fseek(fidr, 0, SEEK_SET);
	int len1 = ftell(fidr);
	fseek(fidr, 0, SEEK_END);
	int len2 = ftell(fidr);
	int Tn = len2 - len1;
	int NN = Tn/2/160;
	fseek(fidr, 0, SEEK_SET);

    int vadout1 = 0; float vadout2 = 0;
    for (int n=0; n<NN; n++)
    {
        size_t value = fread(indata, 2, 160, fidr);
        vadout1 = WebRtcVad_Process(myvad1, 16000, indata, 160);
        myvad2.ProcessChunk(indata, 160, 16000);
        vadout2 = myvad2.last_voice_probability();
        cout<<"frame = "<<n<<endl;
        //cout<<vadout1<<" "<<vadout2<<endl;
        fwrite(&vadout1, 2, 1, fidw[0]);
        fwrite(&vadout2, 4, 1, fidw[1]);
    }

    fclose(fidr);   
    fclose(fidw[0]);
    fclose(fidw[1]);

    return 0;
}
*/


